
<template>
  <div>
    <el-tree :data="data" :props="defaultProps" :expand-on-click-node="false">
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.label }}</span>
        <span>
          <el-button
            v-if="data.catLevel != 3"
            type="text"
            size="mini"
            @click="() => append(data)"
          >
            添加
          </el-button>
          <el-button
            v-if="data.children == null"
            type="text"
            size="mini"
            @click="() => remove(node, data)"
          >
            删除
          </el-button>
          <el-button
            v-if="data.catLevel != 3"
            type="text"
            size="mini"
            @click="() => update(data)"
          >
            修改
          </el-button>
        </span>
      </span>
    </el-tree>

    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="45%"
      :before-close="handleClose"
    >
      <span slot="footer" class="dialog-footer">
        <el-form :model="dataForm" ref="dataForm" label-width="80px">
          <el-form-item label="分类名称" prop="">
            <el-input v-model="dataForm.name" placeholder="分类名称"></el-input>
          </el-form-item>
          <el-form-item label="分类名称" prop="">
            <el-input v-model="dataForm.name2" placeholder=""></el-input>
          </el-form-item>
          <el-radio v-model="dataForm.showStatus" label="0">显示</el-radio>
          <el-radio v-model="dataForm.showStatus" label="1">隐藏</el-radio>
        </el-form>
        <br />
        <br />
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addtree">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      data: [],
      defaultProps: {
        children: "children",
        label: "name",
      },
      dialogVisible: false,
      dataForm: {},
    };
  },
  created() {
    this.getlist();
  },
  methods: {
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    append(data) {
      console.log(data);
      this.dataForm.catId = "";

      console.log(this.dataForm);

      this.dialogVisible = true;
    },
    remove(node, data) {
      let a = [node.data.catId];
      this.$http({
        url: this.$http.adornUrl("/product/category/delete"),
        method: "post",
        data: a,
      }).then(({ data }) => {
        console.log("成功获取到菜单数据:", data);
        this.data = data.page;
        this.getlist();
      });
    },
    getlist() {
      this.dialogVisible = false;
      this.$http({
        url: this.$http.adornUrl("/product/category/tylelist"),
        method: "get",
      }).then(({ data }) => {
        console.log("成功获取到菜单数据:", data);
        this.data = data.page;
      });
    },
    addtree() {
      this.dialogVisible = true;
      console.log("这是获取的值", this.dataForm);
      this.$http({
        url: this.$http.adornUrl("/product/category/addtree"),
        method: "post",
        data: this.dataForm,
      }).then(({ data }) => {
        if (data.data == 1) {
          alert("修改成功！");
        } else {
          alert("添加成功！");
        }

        this.dataForm.name = "";
        this.dataForm.name2 = "";
        this.dataForm.showStatus = "";
        this.dataForm.catId = "";
        this.getlist();
      });
    },
    update(data) {
      console.log(data);
      this.dataForm.catId = data.catId;
      this.dataForm.name = data.name;
      this.dialogVisible = true;
    },
  },
};
</script>